package com.TexasHoldem.xSocket.engine;

import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;


import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.IConnectHandler;
import org.xsocket.connection.IHandler;
import org.xsocket.connection.INonBlockingConnection;



public class AbstractEngine implements IConnectHandler {

	
	static final String CMD_TEST = "gameClient.test";

	protected static final String CMD_VERSION = "gameClient.version";	

	static final String CMD_ERR_VERSION = "gameClient.errorVersion";

	static final String CMD_KICKOUT = "gameClient.kickout";

	static final String CMD_CLOSE = "close";
	
	static final String CMD_WORLD_CHAT = "server.WorldChatMsg";
	
	static final String CMD_ALLIANCE_CHAT = "server.AllianceChatMsg";
	
	static final String CMD_SERVER_SYSTEM_INFO = "server.SystemInfoMsg";
	
	private String version = "1.1.0.0";
	
	private static Log log = LogFactory.getLog(AbstractEngine.class);
	
	private final AtomicInteger connectCount = new AtomicInteger();
	
	protected boolean active = true;
	
	private final AtomicLong sequence = new AtomicLong(0);
	
	private static List<String> internalCmdList = new ArrayList<String>();
	
	private final AtomicLong sendCount = new AtomicLong();

	private final AtomicLong sendTimeCount = new AtomicLong();

	private final AtomicLong sendLen = new AtomicLong();	
	
	
	@Override
	public boolean onConnect(INonBlockingConnection nbc) throws IOException,
			BufferUnderflowException, MaxReadSizeExceededException {
		// TODO Auto-generated method stub
				
		if (log.isDebugEnabled()) {
			log.debug("xServerPool thread=" + Thread.currentThread().getName()
					+ ";ip=" + nbc.getRemoteAddress().getHostAddress());
		}

		if (!active)
			return false;
		connectCount.incrementAndGet();
		IHandler h = createNewHandler(sequence.addAndGet(1), nbc);
		nbc.setHandler(h);

		return true;
	}

	protected IHandler createNewHandler(long index, INonBlockingConnection nbc) throws IOException 
	{
		return new SocketHandler(index, this, nbc);
	}
	
	public static boolean isInternalCmd(String cmd) {
		if (internalCmdList.isEmpty())
			return false;

		for (String c : internalCmdList) {
			if (c.equals(cmd))
				return true;
		}

		return false;
	}
	public void handleInternalRequest(String cmd, Object data,
			SocketHandler handler) {
		
	}

	
	public void handleTestRequest(String cmd, Object data, SocketHandler handler) {
		if (log.isDebugEnabled())
			log.debug("handleTestRequest");

		handler.addResponse(cmd, data);
	}

	public void handleRequest(String cmd, JSONObject data, SocketHandler socketHandler) {
		// TODO Auto-generated method stub
		
		
	}

	public void addSendLen(int len) {
		
		// TODO Auto-generated method stub
		this.sendCount.incrementAndGet();
		this.sendLen.addAndGet(len);
	}
}
